package com.ln.asset.core.controller;


import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ln.asset.common.result.R;
import com.ln.asset.core.pojo.AssetInfo;
import com.ln.asset.core.pojo.RecRecipients;
import com.ln.asset.core.service.RecRecipientsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 资产领用记录表 前端控制器
 * </p>
 *
 * @author cuixiaoxin
 * @since 2022-04-28
 */
@Api(tags = "资产领用管理")
@RestController
@RequestMapping("/admin/core/recRecipients")
@Slf4j
public class RecRecipientsController {
    @Resource
    private RecRecipientsService recRecipientsService;

    /**
     * 资产领用记录表查询 +分页
     */
    @ApiOperation("获取资产领用分页列表")
    @GetMapping("/getRecListPage/{page}/{limit}")
    public R getRecListPage( @ApiParam(value = "当前页码", required = true)
                             @PathVariable Long page,

                             @ApiParam(value = "每页记录数", required = true)
                             @PathVariable Long limit){
        Page<RecRecipients> objectPage = new Page<>(page,limit);
        IPage<RecRecipients> list = recRecipientsService.getRecListPage(objectPage);
        return R.ok().data("list",list);
    }

    /**
     * 删除一条资产领用记录
     */
    @ApiOperation("删除资产领用记录")
    @DeleteMapping("/deleteById/{rId}")
    public R deleteById( @ApiParam(value = "申请单id", required = true)@PathVariable Integer rId){
        try {
            recRecipientsService.deleteById(rId);
            return R.ok().message("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("删除失败");
        }

    }

    /**
     * 增加资产领用申请记录 申请资产领用生成一条记录
     */
    @ApiOperation("增加资产领用申请记录")
    @PostMapping("/insertRec")
    public R insertRec(@ApiParam(value = "领用申请记录", required = true)@RequestBody RecRecipients recRecipients){
        try {
            recRecipientsService.insertRec(recRecipients);
            return R.ok().message("申请成功");
        }catch (Exception e){
            log.error(e.getMessage());
            return R.error().message("申请失败");
        }
    }

    @ApiOperation("根据id查询领用申请单详情")
    @GetMapping("/getRecById/{rId}")
    public R getRecById(@ApiParam(value = "申请单id", required = true) @PathVariable Integer rId){
        RecRecipients recById = recRecipientsService.getRecById(rId);
        return R.ok().data("recRecipients",recById);
    }

    /**
     * 修改资产领用申请单状态  审批
     *
     */
    @ApiOperation("审批申请单")
    @PostMapping("/updateRecStatus")
    public R updateRecStatus(@RequestParam(name = "status") Integer status,
                             @RequestBody(required = false) RecRecipients recipients){
        try {
            recRecipientsService.updateRecStatus(status,recipients);
            return R.ok().message("审批完成");
        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("审批失败");
        }
    }

    /**
     * 全查资产表
     * @return
     */
    @ApiOperation("全查资产表")
    @GetMapping("/selectAllAsset")
    public R selectAllAsset(){
        List<AssetInfo> assetInfo = recRecipientsService.selectAllAsset();
        return R.ok().data("assetInfo",assetInfo);
    }

    /**
     * 发放资产通过
     */
    @ApiOperation("发放资产通过")
    @PostMapping("/pass")
    public R pass(@RequestBody RecRecipients recRecipients,
                  @RequestParam(name = "str") String str){
        String[] split = str.split(",");
        List<String> strings = Arrays.asList(split);
        ArrayList<Integer> objects = new ArrayList<>();
        for (String string : strings) {
            int i = Integer.parseInt(string);
            objects.add(i);
        }
        try {
            recRecipientsService.saveRec(recRecipients);
            recRecipientsService.saveRecZhong(recRecipients.getRId(),objects);
            return R.ok().message("审批通过");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("审批不通过");
        }
    }

    @ApiOperation("根据领用id查询资产列表")
    @GetMapping("/selectRecAndAsset/{rid}")
    public R selectRecAndAsset(@ApiParam(value = "申请单id", required = true) @PathVariable Integer rid){
        List<AssetInfo> assetInfos = recRecipientsService.selectRecAndAsset(rid);
        return R.ok().data("list",assetInfos);
    }

}

